# coding=utf-8
import pymysql
import os
import re
import mysql.connector
# 包全路径
packagepath = r'D:\generate'

# 包名称
packageName = "org.dcexam.cms.module.entity"

# 数据表前缀
prefix = 'exam_'

# 数据表名称 ,传入一个list 集合  请注意，如果有重名的表 那么 会 出错误
# 就算不再同一个数据库也会出错。。。。请谨慎操作
tableNames = []
mydb = mysql.connector.connect(
    host="192.168.47.102",  # 数据库主机地址
    user="ccmadmin",  # 数据库用户名
    database="my_blog",#数据库名称 没有的话插入操作报错
    passwd="211314wccA@"  # 数据库密码

)
mycursor = mydb.cursor()
mycursor.execute("SHOW TABLES")
for x in mycursor:
    #print(type(x))
    #print(x[0])
    tableNames.append(x[0])

host="192.168.47.102"
user="ccmadmin"
password="211314wccA@"
db = "my_blog"
print(tableNames)
for tableName in tableNames:
    # capitalize 可以将首字母大写
    fileName=""
    nameStr=tableName.split("_")
    for tmp in nameStr:
        fileName=fileName+tmp.capitalize()
    fileName = fileName + ".java"
    print
    fileName

    conn = pymysql.connect(host=host, user=user, password=password, database=db, charset="utf8")

    with conn.cursor() as cursor:
        cursor.execute(
            'SELECT COLUMN_NAME, COLUMN_TYPE, COLUMN_COMMENT FROM information_schema.columns WHERE TABLE_NAME="' + tableName + '"')
        rs = cursor.fetchall()
        with open(os.path.join(packagepath, fileName), 'w+') as file:
            file.writelines('''package ''' + packageName + ''';
import lombok.Data;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

@Data
@Table(name = "''' + tableName + '''")
public class ''' + fileName[:-5] + '''{\n''')
            for r in rs:
                print(r)
                col = r[0]  # 字段名
                types = r[1]  # 类型
                javaType = ''
                comment = r[2]  # 注释
                print(types)
                if types.find("tinyint") != -1:
                    javaType = "Boolean "
                elif types.find("int") != -1:
                    javaType = "Integer "
                elif types.find("date") != -1:
                    javaType = "String "
                elif types.find("blob") != -1:
                    javaType = 'String '
                elif types.find("text") != -1:
                    javaType = 'String '
                elif types.find("varchar") != -1:
                    javaType = 'String '
                elif types.find("char") != -1:
                    javaType = 'String '
                elif types.find("float") != -1:
                    javaType = "Float "
                elif types.find("double") != -1:
                    javaType = "Double "
                if (col == 'id' or re.compile('[a-zA-Z0-9]id').search(col)):
                    line = '\t@Id\n\t@GeneratedValue(generator="JDBC")\n\t' + javaType.__str__() + col.__str__() + ' ; //' + comment.__str__() + "\n"
                else:
                    line ='''    ''' + javaType.__str__() + col.__str__() + ' ; //' + comment.__str__()
                file.writelines(line + '\n')
            file.writelines('}')